package com.koron.css2.workflow;

import com.koron.css2.ServerInterface;
import com.koron.css2.clientCenter.bean.AccountInfoBean;
import com.koron.css2.clientCenter.bean.CtmInfoBean;
import com.koron.css2.clientCenter.bean.UserBean;
import com.koron.css2.clientCenter.mapper.AccountInfoMapper;
import com.koron.css2.clientCenter.mapper.CtmInfoMapper;
import com.koron.css2.clientCenter.mapper.UserInfoMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import com.koron.util.Tools;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

public class UserInfoUpdate implements ServerInterface {
    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try{
            String userNos = JsonUtils.objectToPojo(req.getData(), String.class);
            String[] userNoArray = userNos.split(",");
            for (String userNo : userNoArray){
                UserInfoMapper userInfoMapper = factory.getMapper(UserInfoMapper.class);
                CtmInfoMapper ctmInfoMapper = factory.getMapper(CtmInfoMapper.class);
                AccountInfoMapper accountInfoMapper = factory.getMapper(AccountInfoMapper.class);
                UserBean user = userInfoMapper.getUserInfoByUserNo(userNo, UserBean.USER);

                CtmInfoBean ctmInfo = new CtmInfoBean();
                ctmInfo.setClientAddr(user.getCtmAddr());
                ctmInfo.setCtmAddr(user.getCtmAddr());
                ctmInfo.setCtmName(user.getCtmName());
                ctmInfo.setCtmNo(Tools.getSameCtmNo(user.getUserNo()));
                ctmInfo.setClientAddrDetail(user.getCtmAddr());
                ctmInfo.setCreditLevel("1");
                ctmInfo.setCertType(user.getCertType());
                ctmInfo.setCtmType("101");
                ctmInfo.setCertNo(user.getCertNo());
                ctmInfo.setLinkTel(user.getLinkTel());
                ctmInfo.setMobile(user.getMobile());
                ctmInfo.setCreateInfo(userInfo);
                ctmInfoMapper.ctmInfoAdd(ctmInfo, CtmInfoBean.CTM);

                AccountInfoBean accountInfo = accountInfoMapper.getAccountInfoById("666c34c1e4b06e65919e0ca1", AccountInfoBean.ACCOUNT);
                accountInfo.setAccountNo(Tools.getSameAccountNo(user.getUserNo()));
                accountInfo.setCreateInfo(userInfo);
                accountInfoMapper.accountInfoAdd(accountInfo, AccountInfoBean.ACCOUNT);

                user.setCtmNo(ctmInfo.getCtmNo());
                user.setAccountNo(accountInfo.getAccountNo());
                userInfoMapper.userInfoUpdateCtmNo(user.getId(), ctmInfo.getCtmNo(), accountInfo.getAccountNo());
            }
            return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", Void.class);
        }catch (Exception exception){
            logger.error("修改失败：" + exception);
        }
        return MessageBean.create(Constant.MESSAGE_INT_FAIL, "fail", Void.class);
    }
}
